/**
 * 组织机构
 * @param {Object} main
 */
Ext.extend(Bee.module, {
    init: function(main){
        var tree = new Ext.tree.TreePanel({
            id: 'sm-organizationMan-tree',
            region: 'center',
            useArrows: true,
            buttonStateful: false,
            autoScroll: true,
            border: false,
            loader: new Bee.tree.DWRTreeLoader({
                dwrCall: smDwr.findOrgEx,
                nodeMeta: {
                    id: 'sid',
                    text: 'cname',
                    leaf: 'isLeaf'
                }
            }),
            root: {
                nodeType: 'async',
                text: '组织机构',
                draggable: false,
                sid: '0',
                cname: '组织机构',
                leaf: false,
                expanded: true
            },
            listeners: {
                click: function(node, e){
                    if (node.isRoot) {
                        infoForm.getForm().reset();
                        Ext.getCmp('sm-organizationMan-infoForm-saveBtn').enable();
                        Ext.getCmp('sm-organizationMan-infoForm-updateBtn').disable();
                        Ext.getCmp('sm-organizationMan-infoForm-resetBtn').enable();
                        Ext.getCmp('sm-organizationMan-infoForm-deleteBtn').disable();
                    }
                    else {
                        infoForm.load(node.attributes.sid);
                        if (node.attributes.isLeaf) {
                            Ext.getCmp('sm-organizationMan-infoForm-saveBtn').disable();
                        }
                        else {
                            Ext.getCmp('sm-organizationMan-infoForm-saveBtn').enable();
                        }
                        Ext.getCmp('sm-organizationMan-infoForm-updateBtn').enable();
                        Ext.getCmp('sm-organizationMan-infoForm-resetBtn').enable();
                        Ext.getCmp('sm-organizationMan-infoForm-deleteBtn').enable();
                    }
                }
            }
        });
        
        var infoForm = new Bee.form.DWRForm({
            id: 'sm-organizationMan-infoForm',
            region: 'east',
            cls: 'x-bee-form-left',
            width: 400,
            labelWidth: 80,
            action: 'save',
            dwr: {
                save: smDwr.saveOrganization,
                update: smDwr.updateOrganization,
                load: smDwr.getOrganization
            },
            afterSubmit: function(){
                tree.getRootNode().reload();
                infoForm.getForm().reset();
            },
            items: [{
                fieldLabel: 'sid',
                name: 'sid',
                xtype: 'hidden'
            }, {
                fieldLabel: 'status',
                name: 'status',
                xtype: 'hidden',
                value: 1
            }, {
                fieldLabel: 'smOrganizationSid',
                name: 'smOrganizationSid',
                xtype: 'hidden'
            }, {
                fieldLabel: 'parentSid',
                name: 'parentSid',
                xtype: 'hidden'
            }, {
                fieldLabel: '编码',
                name: 'ename',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '名称',
                name: 'cname',
                allowBlank: false,
                maxLength: 100
            }, {
                fieldLabel: '是否叶子节点',
                hiddenName: 'isLeaf',
                xtype: 'combo',
                value: '0',
                editable: false,
                mode: 'local',
                triggerAction: 'all',
                store: [['1', '是'], ['0', '否']]
            }, {
                fieldLabel: '备注',
                name: 'comments',
                xtype: 'textarea',
                allowBlank: true,
                maxLength: 100
            }, {
                fieldLabel: '排序',
                name: 'sort',
                xtype: 'numberfield',
                allowDecimals: false,
                allowNegative: false,
                allowBlank: false,
                maxLength: 3
            }, {
                layout: 'hbox',
                xtype: 'panel',
                bodyCssClass: 'x-bee-form-hbox',
                layoutConfig: {
                    padding: '5',
                    pack: 'center',
                    align: 'middle'
                },
                defaults: {
                    minWidth: 75,
                    xtype: 'button',
                    hidden: false,
                    margins: '0 10 0 0'
                },
                items: [{
                    id: 'sm-organizationMan-infoForm-saveBtn',
                    text: '新增',
                    iconCls: 'btn_insert',
                    disabled: true,
                    handler: function(){
                        var node = tree.getSelectionModel().getSelectedNode();
                        if (!node) {
                            Bee.msg('请选择上级菜单!');
                            return false;
                        }
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm.getForm().setValues({
                                    parentSid: node.attributes.sid
                                });
                                infoForm['save']();
                            }
                        });
                    }
                }, {
                    id: 'sm-organizationMan-infoForm-updateBtn',
                    text: '修改',
                    iconCls: 'btn_modify',
                    disabled: true,
                    handler: function(){
                        Ext.Msg.confirm('提示', '确认保存吗?', function(param){
                            if (param == 'yes') {
                                infoForm['update']();
                            }
                        });
                    }
                }, {
                    id: 'sm-organizationMan-infoForm-resetBtn',
                    text: '刷新',
                    iconCls: 'btn_refresh',
                    disabled: true,
                    handler: function(){
                        var node = tree.getSelectionModel().getSelectedNode();
                        if (!node || node.getDepth() === 0) {
                            infoForm.getForm().reset();
                        }
                        else {
                            infoForm.load(node.attributes.sid);
                        }
                    }
                }, {
                    id: 'sm-organizationMan-infoForm-deleteBtn',
                    text: '删除',
                    iconCls: 'btn_delete',
                    disabled: true,
                    handler: function(){
                        var node = tree.getSelectionModel().getSelectedNode();
                        Ext.Msg.confirm('提示', '确认删除吗?', function(param){
                            if (param == 'yes') {
                                smDwr.deleteOrganization(node.id, function(){
                                    Bee.msg('删除成功！');
                                    tree.getRootNode().reload();
                                });
                            }
                        });
                    }
                }]
            }]
        });
        
        var panel = new Ext.Panel({
            id: 'sm-organizationMan-panel',
            layout: 'border',
            border: false,
            items: [tree, infoForm]
        });
        
        this.main.associatedComp = [];
        this.main.add(panel);
        this.main.doLayout();
    }
});
